package com.tinyseed.emcp.api.crawler.service.utils;

import cn.hutool.json.JSONObject;
import com.tinyseed.emcp.api.exception.entity.ExceptionInfo;
import com.tinyseed.emcp.api.exception.state.NewState;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @version Revision 1.0.0
 * @版权：
 * @author: Howard
 * @email:
 * @see:
 * @创建日期：2020/3/13
 * @功能说明：ExceptionInfo转换类
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2020/3/13 wuxiran        创建
 * @end
 */
public class ExceptionInfoMapper {

    public static List<ExceptionInfo> convertExceptionInfo(List<JSONObject> list) {
        List<ExceptionInfo> exceptionInfos = new ArrayList<>();
        list.forEach(item -> exceptionInfos.add(ExceptionInfo.builder()
                .createTime(parseTimeStamp((String) item.get("CreateTime")))
                .currentStatus(new NewState().getStateName())
                .creator("Ja")
                .currentStatusCode(new NewState().getStateCode())
                .instrumentAddress((String) item.get("MeterId"))
                .jaId(Long.valueOf((String) item.get("Id")))
                .organizeId(item.get("Id").toString())
                .organizeName((String) item.get("OrganizeName"))
                .description((String) item.get("Description"))
                .eventType((String) item.get("EventType"))
                .remark(Optional.ofNullable(item.get("Remark")).map(Object::toString).orElse(""))
                .equipmentType((String) item.get("EquipmentName"))
                .monitorId(Long.valueOf((String) item.get("MonitorId")))
                .enterpriseId(Long.valueOf((String) item.get("EnterpriseId")))
                .enterpriseName((String) item.get("EnterpriseName"))
                .swicthSn((String) item.get("SwicthId"))
                .monitorName((String) item.get("MonitorName"))
                .shopName((String) item.get("ShopName"))
                .build()));

        return exceptionInfos;

    }

    public static Timestamp parseTimeStamp(String s) {
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(s);
        String sd = m.replaceAll("");

        return new Timestamp(Long.valueOf(sd));
    }

}
